import os

class FilenameReplacer:
    """
    批量替换文件名中的字符串
    """

    def __init__(self, folder_path, replacements):
        """
        :param folder_path: 目标文件夹
        :param replacements: 替换规则列表 [(旧, 新), ...]
        """
        self.folder_path = folder_path
        self.replacements = replacements

    def replace_in_file(self, filename):
        """
        对单个文件名应用替换规则
        """
        new_filename = filename
        for old, new in self.replacements:
            new_filename = new_filename.replace(old, new)
        return new_filename

    def process(self):
        """
        遍历文件夹，批量重命名文件
        """
        for filename in os.listdir(self.folder_path):
            old_path = os.path.join(self.folder_path, filename)
            if not os.path.isfile(old_path):
                continue

            new_filename = self.replace_in_file(filename)
            if new_filename != filename:
                new_path = os.path.join(self.folder_path, new_filename)
                os.rename(old_path, new_path)
                print(f"重命名: {filename} -> {new_filename}")


if __name__ == "__main__":
    folder = input("请输入文件夹路径: ").strip()
    rules_input = input("请输入要替换的规则（格式：旧->新，用逗号分隔）: ").strip()

    # 解析规则
    replacements = []
    for rule in rules_input.split(","):
        if "->" in rule:
            old, new = rule.split("->", 1)
            replacements.append((old.strip(), new.strip()))

    replacer = FilenameReplacer(folder, replacements)
    replacer.process()
